<?php

class BlogScraperOld
{
    var $download = false;
    var $recursive = false;
    
    function __construct($abbrev, $url)
    {
        $sites = Database::query("SELECT * FROM `sites` WHERE `abbrev` LIKE '$abbrev'");
        $this->row = $sites[0];
        echo "Scraping: ".$sites[0]['display_name'];
        $html_text = file_get_contents($url ? $url : $this->row['url']);

        $this->source = $html_text;
        $this->url = $url ? $url : $this->row['url'];
        $this->html = new DomDocument();
        $this->html->preserveWhiteSpace = false;
        
        if ($this->row['format'] == "xml") $this->html->loadXML($html_text);
        else @$this->html->loadHTML($html_text);
        $this->xpath = new DomXPath($this->html);
    }
    
    function setURL($url)
    {
        $html_text = file_get_contents($url);
        $this->source = $html_text;
        $this->url = $url;
        $this->html = new DomDocument("1.0", "ISO-8859-15" );
        $this->html->preserveWhiteSpace = false;
        @$this->html->loadHTML($html_text);
        
        $this->xpath = new DomXPath($this->html);        
    }
    
    function getPosts()
    {
        $posts = $this->xpath->query("//html//body//div[@class='post']");  
        if ($posts->length > 0) return $posts;
        else return $this->xpath->query("//html//body//div[@class='entry']"); 
    }
    
    function getDate($post)
    {
        $date_node = $this->xpath->query("//html//body//div[@class='date']")->item($this->post_index)->nodeValue;   
        $date_node = str_replace('|', '', $date_node);
        if (strlen($date_node) > 4) 
        {
            $date = date('Y-m-d', strtotime($date_node));
            return $date;
        }
    }
    
    function getPostLinks($post)
    {
        return $post->getElementsByTagName('a');
    }
    
    function getText($post)
    {
        $text = $post->getElementsByTagName('p')->item(0)->nodeValue; 

        return $text;
    }
    
    function getTitle($post)
    {
        $text = $post->getElementsByTagName('h2')->item(0)->getElementsByTagName('a')->item(0)->nodeValue;

        return $text;
    }
    
    function getLink($post)
    {
        $text = $post->getElementsByTagName('h2')->item(0)->getElementsByTagName('a')->item(0)->getAttribute('href'); 

        return $text;
    }
       
    function scrape()
    {
        $posts = $this->getPosts();
        
        echo "<br/>Found $posts->length posts<br/>";
        
        $scraped = 0;
        $this->post_index = 0;
               
        foreach ($posts as $post)
        {  
            $links = $this->getPostLinks($post);
            echo "-----------------------------<br/>";
            echo "Found $links->length links on post.";
            $date = $this->getDate($post);
            $text = $this->getText($post);
            $title = $this->getTitle($post);
            $link = $this->getLink($post);
            
            $post_exists = Database::query(
            "SELECT * FROM `posts` WHERE `title` LIKE '$title' OR `link` LIKE '$link'");
            
            if (!$post_exists[0]['id'])
            {
                $text = mysql_escape_string($text);
                $title = mysql_escape_string($title);
                $link = mysql_escape_string($link);
                
                $site_id = $this->row['id'];
                echo "New post: ".$title;
                $post_id = Database::insert(
                "INSERT INTO `posts`
                 (`site_id`, `title`, `link`, `text`, `date`)
                 VALUES
                 ($site_id, '$title', '$link', '$text', '$date') 
                ");
                echo ' id:'.$post_id;
            }
            else 
            {
                $post_id = $post_exists[0]['id'];
                echo "Found existing post id:".$post_id;
            }
            
            foreach ($links as $link)
            {  
                if ($this->processLink($link->getAttribute('href'), $link->nodeValue, $date, $post_id)) $scraped++;
                if ($scraped > 3) continue;;
            }
            
            $objects = $this->getObjects($post);
            $oject_links = $this->getObjectLinks($objects);
            
            foreach ($oject_links as $link)
            {  
                if ($this->processLink($link, $link, $date, $post_id)) $scraped++;
                if ($scraped > 3) return;
            }
            
            $this->post_index++;
        }
    }

    function getObjects($post)
    {
        $objects = $this->xpath->query("//div[@class='post'][$this->post_index]//object");
        
        foreach ($objects as $object)
        {
            foreach ($object->getElementsByTagName('param') as $embed)
            {
                echo $embed->getAttribute('value');
            }
        }

        return array();
    }     
    
    function getObjectLinks($objects)
    {
        $urls = array();
        
        foreach ($objects as $object)
        {
            $urls[] = $object['soundFile'];
        }
        
        return $urls;
    }
    
    function processLink($href, $name, $date, $post_id)
    {
        $href_parts = explode('.', $href);
        $extension = $href_parts[count($href_parts)-1];
        
        if (strtolower($extension) == "mp3") 
        {
            echo '<b>'.$name.'</b> ['.$href.']<br/>';
            $name_parts = explode('-', $name);
            
            if (count($name_parts) == 2)
            {
                $artist = $name_parts[0];
                $song = $name_parts[1];
                $name = trim($artist).'-'.trim($song);
            }
        
            
            $name = str_replace("“", "", $name);
            $name = str_replace("”", "", $name);
        
            $track = array(
                'name' => strtolower($name),
                'url' => $href,
                'artist' => strtolower($artist),
                'song' => strtolower($song),
                'date' => $date,
                'post_id' => $post_id,
            );
            $track = $this->prepareTrack($track);
            
            if ($this->grabTrack($track)) return true;
            
        }
    }
    
    function prepareTrack($track)
    {
        return $track;
    }
    
    function getLinks()
    {
        return $this->xpath->query("//html//body//a");   
    }
    
    function grabTrack($track)
    {
        $track_url = $track['url'];
        $track['url'] = mysql_real_escape_string($track['url']);
        $track['text'] = mysql_real_escape_string(trim($track['text']));
        $track['name'] = mysql_real_escape_string(trim(self::sanitize($track['name'])));
        $track['name'] = str_replace(".mp3", "", $track['name']);
        
        @$track['artist'] = mysql_real_escape_string(self::sanitize($track['artist']));
        @$track['song'] = mysql_real_escape_string(self::sanitize($track['song']));
        $track['source_url'] = mysql_real_escape_string($this->url);
        @$track['source_name'] = mysql_real_escape_string(trim($track['source_name']));
        $site_id = $this->row['id'];
        if (!$track['post_id']) $track['post_id'] = 0;
        
        $track['name'] = str_replace("“", "", $track['name']);
        $track['name'] = str_replace("”", "", $track['name']);
        
        $track_exists = Database::query("SELECT * FROM `tracks` WHERE `url` LIKE '$track[url]'");
        
        if ($track_exists) 
        {
            echo "Found track '$track[url]' id: ".$track_exists[0]['id'].'name:'.$track_exists[0]['name'].'<br/>';
            $track_id = $track_exists[0]['id'];
            $inserted = false;
        }     
        else
        {
            $track_id = Database::insert(
            "INSERT INTO `tracks` 
            (`name`, `url`, `artist`, `song`, `source_name`, `source_url`, `site_id`, `date`, `post_id`) 
            VALUES ('{$track[name]}', '{$track[url]}', '{$track[artist]}', '{$track[song]}', '{$track[source_name]}', '{$track[source_url]}', {$site_id}, '{$track[date]}', '{$track[post_id]}')");
        
            echo "New track!: [$track[name]] id: ".$track_id.'<br/>';
            var_dump($track).'<br/><br/>';
            $inserted = true;
        }
        
        $track_row = Database::query("SELECT * FROM `tracks` WHERE `id` = $track_id");

        if ($this->download && !file_exists("tracks/".$track_id.".mp3"))
        {

            $track_data = file_get_contents(str_replace(" ", "%20", $track['url']));
        
            $fh = fopen("app/webroot/tracks/".$track_id.".mp3", 'w') or die("can't open file");
            echo "saved";
            fwrite($fh, $track_data);
            fclose($fh);
        }
        
        return $inserted;
    }
    
    function sanitize($str)
    {
    return ereg_replace("[^A-Za-z0-9 '\":,>$./-_()-]", "", trim($str));
    }
    
    function sanitizeOnlyNumeric($str)
    {
    return strtolower(ereg_replace("[^0-9.]", "", trim($str)));
    }
    
    function sanitizeURLSafe($str)
    {
    return ereg_replace("[^A-Za-z0-9 ',>$.:/_-]", "", trim($str));
    }
}

?>